package com.seeyon.kk.utils;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import com.seeyon.apps.kk.util.JdbcAgentUtil;
import com.seeyon.ctp.common.log.CtpLogFactory;
import jersey.repackaged.com.google.common.collect.Maps;

public class KkEmailStrongUtils {

    private static final Log LOGGER = CtpLogFactory.getLog(KkEmailStrongUtils.class);

    /**
     * 人员id 转姓名 和 编码
     *
     * @return
     */
    private String memberIdToNameOrCode() {
        return "SELECT NAME,CODE,EXT_ATTR_2 AS mail,EXT_ATTR_3 AS phone  FROM ORG_MEMBER WHERE ID =:id";
    }

    /**
     * 部门id 转 部门名称
     *
     * @return
     */
    private String deptIdToName() {
        return "SELECT NAME FROM ORG_UNIT WHERE ID =:id";
    }

    /**
     * 枚举id 转 显示名称
     *
     * @return
     */
    public String enumToName() {
        return "SELECT SHOWVALUE as NAME FROM CTP_ENUM_ITEM WHERE ID =:id";
    }

    /**
     * 获取部门下的人员邮箱
     *
     * @return
     */
    private String deptToMemberMail() {
        return "SELECT EXT_ATTR_2 as NAME FROM ORG_MEMBER WHERE ORG_DEPARTMENT_ID =:id";
    }

    /**
     * 获取转换后的内容
     *
     * @param functionContext
     * @param repContent
     * @return
     */
    public String getContext(String functionContext, String repContent) {
        String context = "";
        String sql = "";
        String[] repContents = repContent.split(",");

        switch (functionContext) {
            case "人员转姓名":
                sql = memberIdToNameOrCode();
                break;
            case "人员转编码":
                sql = memberIdToNameOrCode();
                break;
            case "枚举转文本":
                sql = enumToName();
                break;
            case "部门转名称":
                sql = deptIdToName();
                break;
            case "部门人员邮箱":
                sql = deptToMemberMail();
                break;
            case "人员转邮箱":
                sql = memberIdToNameOrCode();
                break;
            case "人员转电话":
                sql = memberIdToNameOrCode();
                break;
        }
        try {
            for (int i = 0; i < repContents.length; i++) {
                Map<String, Object> params = Maps.newHashMap();
                params.put("id", repContents[i]);
                List<Map> datList = JdbcAgentUtil.doQuery(sql, params);
                if (!datList.isEmpty()) {
                    Map<String, Object> map = datList.get(0);
                    if ("人员转编码".equals(functionContext)) {
                        if (i < repContents.length - 1) {
                            context += MapUtils.getString(map, "code") == null ? ""
                                    : MapUtils.getString(map, "code") + ",";
                        } else {
                            context += MapUtils.getString(map, "code") == null ? "" : MapUtils.getString(map, "code");
                        }

                    } else if ("部门人员邮箱".equals(functionContext)) {
                        for (int mailSize = 0; mailSize < datList.size(); mailSize++) {
                            context += MapUtils.getString(map, "name") + ";";
                        }
                    } else if ("人员转邮箱".equals(functionContext)) {
                        context += MapUtils.getString(map, "mail") + ";";
                    } else if ("人员转电话".equals(functionContext)) {
                        context += MapUtils.getString(map, "phone") + ";";
                    } else if ("人员转姓名".equals(functionContext)) {
                        if (i < repContents.length - 1) {
                            context += MapUtils.getString(map, "name") + ",";
                        } else {
                            context += MapUtils.getString(map, "name");
                        }
                     // TODO 如需添加自定义函数，请追加验证
                    } else {
                        if (i < repContents.length - 1) {
                            context += MapUtils.getString(map, "name") + ",";
                        } else {
                            context += MapUtils.getString(map, "name");
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("#强生客开，邮件增强函数转换错误：", e);
        }
        return context;
    }
}
